//
// Created by Administrator on 2021/11/10.
// May Saint Diana bless you!
//
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<string> findAndReplacePattern(vector<string> &words, string pattern) {
        vector<string> ans;
        transform(pattern.begin(), pattern.end(), pattern.begin(), ::toupper);  // pattern变为大写
        for (string &word:words) {
            if (check(word, pattern)) {
                ans.push_back(word);
            }
        }
        return ans;
    }

    /**
     * 判断两个string的模式是不是相同的
     * 亦即  两个string中的char映射是不是一致的
     * @param a 
     * @param p 
     * @return 
     */
    bool check(string &a, string &p) {
        unordered_map<char, char> um;
        for (int i = 0; i < a.size(); ++i) {
            if (!um.count(a[i]) && !um.count(p[i])) {
                um[a[i]] = p[i];
                um[p[i]] = a[i];
            } else {
                if (um.count(a[i]) && um[a[i]] != p[i])
                    return false;
                if (um.count(p[i]) && um[p[i]] != a[i])
                    return false;
            }
        }
        return true;
    }

    bool match(const string &s, const string &p) {
        vector<int> index1(126, -1), index2(126, -1);
        for (int i = 0; i < s.size(); i++) {
            if (index1[s[i]] != index2[p[i]])
                return false;
            index1[s[i]] = index2[p[i]] = i; // 设置映射 具体是多少没关系，只要两个映射连通就行
        }
        return true;
    }
};

int main() {
    vector<string> words{"abc", "deq", "mee", "aqq", "dkd", "ccc"};
    Solution solution;
    auto ans = solution.findAndReplacePattern(words, "abb");
    for (const auto &x:ans) cout << x << endl;
    return 0;
}


